﻿using System;
using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;
using MySql.Data.MySqlClient;
using OrionApplication.OrionLib.Orion;

namespace OrionApplication.OrionLib.Dao
{
    public class DaoOrionGroup : DaoBase
    {
        public DaoOrionGroup()
            : base()
        {
            this.TableName = "OrionGroup";
            this.ViewName = "VOrionGroup";
        }

        public OrionGroup SelectById(Int32 OrionId)
        {
            lParam.Clear();
            OrionGroup retour = new OrionGroup();
            String req = this.DebutSelect
                + this.TableName
                + " WHERE OrionId=@orionid";

            lParam.Add(new MySqlParameter("@orionid", OrionId));

            MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);

            if (dr.Read())
                retour = ChargeFromReader(dr);
            dr.Close();
            return retour;
        }

        public List<OrionGroup> SelectAll(bool excludeInactif = false)
        {
            lParam.Clear();
            List<OrionGroup> retour = new List<OrionGroup>();
            String req = this.DebutSelect
                + this.TableName;

            if (excludeInactif)
            {
                req += " WHERE OrionEtat = 1";
            }

            MySqlDataReader dr = this.ExecuteQueryReader(req);

            while (dr.Read())
                retour.Add(ChargeFromReader(dr));
            dr.Close();
            return retour;
        }

        public OrionGroup Insert(OrionGroup om)
        {
            lParam.Clear();

            om.OrionCrea = DateTime.Now;
            om.OrionMaj = DateTime.Now;

            OrionGroup retour = new OrionGroup();
            String req = "INSERT INTO " + TableName + "(OrionId, Libelle, TypeId, OrionCrea, OrionMaj, OrionUserId, OrionEtat) " +
                "VALUES(NULL, @Libelle, @TypeId, @OrionCrea, @OrionMaj, @OrionUserId, @OrionEtat)";

            lParam.Add(new MySqlParameter("@Libelle", om.Libelle));
            lParam.Add(new MySqlParameter("@TypeId", om.TypeId));
            lParam.Add(new MySqlParameter("@OrionCrea", om.OrionCrea));
            lParam.Add(new MySqlParameter("@OrionMaj", om.OrionMaj));
            lParam.Add(new MySqlParameter("@OrionUserId", ((OrionUser)CallContext.GetData(OrionConfBase.OrionUserConnected)).OrionId));
            lParam.Add(new MySqlParameter("@OrionEtat", 1));

            this.ExecuteNonQuery(req, lParam);
            retour = this.SelectById(Convert.ToInt32(this.LastInsertId));

            return retour;
        }

        public OrionGroup Update(OrionGroup om)
        {
            om.OrionMaj = DateTime.Now;

            lParam.Clear();

            OrionGroup retour = new OrionGroup();
            String req = "UPDATE " + TableName + " SET Libelle=@Libelle, TypeId=@TypeId, OrionMaj=@OrionMaj, OrionUserId=@OrionUserId " +
                " WHERE OrionId=@OrionId";

            lParam.Add(new MySqlParameter("@OrionId", om.OrionId));
            lParam.Add(new MySqlParameter("@Libelle", om.Libelle));
            lParam.Add(new MySqlParameter("@TypeId", om.TypeId));
            lParam.Add(new MySqlParameter("@OrionMaj", om.OrionMaj));
            lParam.Add(new MySqlParameter("@OrionUserId", ((OrionUser)CallContext.GetData(OrionConfBase.OrionUserConnected)).OrionId));

            this.ExecuteNonQuery(req, lParam);
            retour = this.SelectById(om.OrionId);

            return retour;
        }

        private OrionGroup ChargeFromReader(MySqlDataReader dr)
        {
            OrionGroup retour = new OrionGroup();
            retour.OrionId = dr.GetInt32("OrionId");

            if (dr.IsDBNull(1))
                retour.Libelle = string.Empty;
            else
                retour.Libelle = dr.GetString("Libelle");
            if (dr.IsDBNull(2))
                retour.TypeId = 0;
            else
                retour.TypeId = dr.GetInt32("TypeId");

            retour.OrionCrea = dr.GetDateTime("OrionCrea");
            retour.OrionMaj = dr.GetDateTime("OrionMaj");
            retour.OrionUserId = dr.GetInt32("OrionUserId");
            retour.OrionEtat = dr.GetBoolean("OrionEtat");

            return retour;
        }
    }
}
